package org.yn.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yn.auth.dto.LoginDTO;
import org.yn.auth.service.ILoginService;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.LoginContext;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
public class LoginController {
    @Autowired
    ILoginService loginService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDTO dto){
        try {
            Map<String,Object> map = loginService.login(dto);
            return AjaxResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @PutMapping("/LoginOut")
    public AjaxResult LoginOut(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            LoginContext.loginMap.remove(token);
            LoginContext.userAuth.clear();
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
